package servlet;

import util.JdbcUtil;

import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doGet(req, resp);
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       // super.doPost(req, resp);
        String username =  req.getParameter("username");
        String password =  req.getParameter("password");
        String sql = "select * from user where username = ? and password = ?";
        try {
            PreparedStatement ps =  JdbcUtil.getConnection().prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            ps.setString(1, username);
            ps.setString(2, password);
            ResultSet set = ps.executeQuery();
            if(set.next()){
                int id=set.getInt(1);
                resp.sendRedirect("homepage?id="+id);
            }else{
                resp.sendRedirect("fill.jsp");
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
